/**
 * Created by albert on 17/5/2.
 */
import {FormGroup, FormControl} from '@angular/forms';
import {Injectable} from '@angular/core';

export function mobileValidator(control: FormControl): any {
  let reg = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0-9])|(17[0-9]))+\d{8}$/;
  let valid = reg.test(control.value);
  if (valid)
    console.log('手机号校验结果：', valid);
  return valid ? null : {mobile: true};
}
export function passwordValidator(control: FormControl): any {
  let reg = /^[0-9a-zA-Z]{6,16}$/;
  let valid = reg.test(control.value);
  if (valid)
    console.log('密码验结果：', valid);
  return valid ? null : {password: true};
}

export function equalValidator(group: FormGroup): any {
  let password = group.get('password') as FormControl;
  let pwdCheck = group.get('pwdCheck') as FormControl;
  let valid: boolean = (password.value === pwdCheck.value);
  if (valid)
    console.log('密码确认是否一致：', valid);
  return valid ? null : {equal: true};
}

export function verifyValidator(control: FormControl): any {
  let reg = /^[0-9a-zA-Z]{6}$/;
  let valid = reg.test(control.value);
  if (valid)
    console.log('密码验结果：', valid);
  return valid ? null : {verify: true};
}
export function nameValidator(control: FormControl): any {
  let reg = /^[\u4e00-\u9fa5]{2,4}$|^[a-zA-Z]{1,30}$/g;
  let valid = reg.test(control.value);
  if (valid) {
    console.log('姓名验证结果：', valid);
    return valid ? null : {lesseeName: true};
  }else{
    return {lesseeName:false};
  }
}
export function peopleIdValidator(control: FormControl): any {
  let reg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
  let valid = reg.test(control.value);
  if (valid) {
    console.log('身份证验证结果：', valid);
    return valid ? null : {lesseePeopleId: true};
  }else{
    return {lesseePeopleId:false};
  }
}
